<?php

use swap\visitor;

class language_model extends swap\rdb_model {
    
    public static $categories = array(
        0 => '无',
        1 => '英语',
        2 => '日语',
        3 => '法语',
        4 => '德语',
        5 => '俄语',
        6 => '韩语',
        7 => '西班牙语',
        8 => '葡萄牙语',
        9 => '阿拉伯语',
        10 =>'意大利语',
        999 => '其他',
    );
    
    public static $write_skills = array(
        0 => '一般',
        1 => '良好',
        2 => '熟练',
        3 => '精通',
    );
    
    public static $read_skills = array(
        0 => '一般',
        1 => '良好',
        2 => '熟练',
        3 => '精通',
    );
    
    public static function prop_rules($operation) {
        $category = array(
            'category' => array(
                'change_to' => 'int',
                'cannot_be' => array(0, '请选择相关语种'),
                'callback' => function($category) {
                    $categories = language_model::get_existed_category(visitor::get_value('id'));
                    if (in_array($category, $categories)) {
                        return '亲,您已经添加了' . language_model::$categories[$category];
                    }
                }
            ),
        );
            
        if ($operation === 'edit') {
            unset($category['category']['callback']);
        }
        
        $write_skill = array(
            'write_skill' => array(
                'change_to' => 'int',
            ),
        );
        
        $read_skill = array(
            'read_skill' => array(
                'change_to' => 'int',
            ),
        );
        
        $rules = array(
            'new' => $category + $write_skill + $read_skill,
            'edit' => $category + $write_skill + $read_skill,
        );
        
        return $rules[$operation];
    }
 
    public static function get_existed_language($resume_id) {
        $languages = language_model::get(array('resume_id' => $resume_id), array(array('id' => 'desc'), 0, resume_ext_service::$limits['language']));
        return $languages;
    }
    
    public static function get_existed_category($resume_id) {
        $categories = array();
        
        $languages = self::get_existed_language($resume_id);
        if ($languages !== null) {
            foreach ($languages as $language) {
                $categories[] = $language->category;
            }
        }
        
        return $categories;
    }
    
}